package heliotrope.res
{
	import flash.display.BitmapData;
	import flash.utils.Dictionary;
	
	import heliotrope.global.ErrorHandler;
	import heliotrope.net.loader.IStateTarget;
	import heliotrope.net.loader.LoadState;

	public class BitmapdataDistributor implements IResoureceDistributor,IStateTarget
	{
		private var _loadState:String;
		private var _recievers:Dictionary;
		private var _bitmapData:BitmapData;
		public function BitmapdataDistributor()
		{
			_loadState = LoadState.NOT_LOAD;
		}

		public function saveData(data:Object):void
		{
			var check:Boolean = data is BitmapData;
			if(!check)
			{
				CONFIG::debug
				{
					try
					{
						throw new ArgumentError("Wrong data type.");
					} 
					catch(error:ArgumentError) 
					{
						ErrorHandler.handle(error);
					}
				}
			}
			_bitmapData = data as BitmapData;
			if(_recievers)
			{
				refreshChildren(_bitmapData);
			}
		}
		
		private function refreshChildren(value:BitmapData):void
		{
			for each(var child:IResourceAttacher in _recievers)
			{
				child.recieve(value);
			}
		}
		public function attach(reciever:IResourceAttacher):void
		{
			if(!_recievers)
			{
				_recievers = new Dictionary(true);
			}
			if(_bitmapData)
			{
				reciever.recieve(_bitmapData)
			}
			_recievers[reciever] = reciever;
		}
		
		public function detach(reciever:IResourceAttacher):Boolean
		{
			return delete _recievers[reciever];
		}
		
		public function detachAll():void
		{
			_recievers = null;
		}
		public function dispose():void
		{
			if(!_bitmapData) return;
			_bitmapData.dispose();
			_bitmapData = null;
		}
		
		public function get loadState():String
		{
			return _loadState;
		}
		
		public function set loadState(value_:String):void
		{
			_loadState = value_;
		}
		
	}
}